#include <wx/wx.h>
#include <wx/tokenzr.h>

#include "highlight.h"

Formatter::Formatter() {
	wxLogMessage(_T("HighlightManager::HighlightManager"));
	
	list_reg.Compile(_T("(^\\s*)(\\d+\\.)(.*)"), wxRE_ADVANCED); //  1. dsak
	pronoun_reg.Compile(_T("\\[([^\\]]+)\\]"), wxRE_ADVANCED); // [prounou]
	synonym_reg.Compile(_T("([^[:alpha:]]+|\\A)(动变|名复|同义|同义参见|反义|反义参见)\\s*(:|：)"), wxRE_ADVANCED);
	verb_reg.Compile(_T("([^[:alpha:]]+|\\A)(ph\\.|n\\.|vi\\.|vi\\.|vt\\.|int\\.|v\\.intr\\.|interj\\.|suf\\.|abbr\\.)"), wxRE_ADVANCED); // vi. vi. vt. int. v.intr. interj. 

	//wxString explain(_("1.dasd"));
	//wxString explain(_("n.dsak132 vi. dksa"));
	
	//wxMessageBox(format(explain));
}


wxString& Formatter::format(const wxString& dict, const wxString& word, wxString& explain) {
	explain.Replace(_T("\\n"), _T("<br>"));

	wxString retstr;
	retstr.Printf(_T("<html><body><font face=\"verdana\"> \
    		<font color=\"#5279AD\"><b>[</b>%s<b>]</b></font>\
    		<font size=\"+3\" color=\"#5279AD\"> <center> <b>%s</b> </center> </font> <br><br>"), \
    		dict.c_str(), word.c_str()
    	);
	
	wxString tempstr;
	
	int i = 0;
	int index = explain.find(_("<br>"),i);
	while ( wxNOT_FOUND!=index ) {
		tempstr = explain.Mid(i, index+4-i);
		//wxMessageBox(tempstr);
	    
		list_reg.Replace(&tempstr, _T("<p>\\1\\2</b><font color=\"#008080\">\\3</font></p>"));
		pronoun_reg.Replace(&tempstr, _T("[<b><font color=\"#0000ff\" face=\"lucida sans unicode\">\\1</font></b>]"));
		synonym_reg.Replace(&tempstr, _T("\\1<b><font color=\"#01259A\">\\2\\3</font></b>"));
		verb_reg.Replace(&tempstr, _T("\\1<em>\\2</em>"));
		/*if (pronoun_reg.Matches(tempstr)) {
			tempstr.Prepend(_T("<b>"));
			tempstr.Append(_T("</b>"));
			//wxMessageBox(tempstr);
		}*/
		
	    i = index+4;
	    index = explain.find(_("<br>"),i);
	    
	    retstr.Append(tempstr);
	}

	tempstr = explain.Mid(i);
	//wxMessageBox(tempstr);

	list_reg.Replace(&tempstr, _T("\\1<b>\\2</b><font color=\"#008080\">\\3</font>"));
	pronoun_reg.Replace(&tempstr, _T("[<b><font color=\"#0000ff\" face=\"lucida sans unicode\">\\1</font></b>]"));
	synonym_reg.Replace(&tempstr, _T("\\1<b><font color=\"#01259A\">\\2\\3</font></b>"));
	verb_reg.Replace(&tempstr, _T("\\1<em>\\2</em>"));
	
    retstr.Append(tempstr);
	
    explain = retstr; //# DEA600
    explain.Append(_T("</font></body></html>"));
    
    
	//list_reg.Replace(&explain, _T("\1<br>\2"));
	
	//wxMessageBox(retstr);
	return explain;
}

Formatter* Formatter::_hlight_mgr=NULL;
